package com.heima.wemedia.feign;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.feign.wemedia.IWemediaClient;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.service.GreenScanService;
import com.heima.wemedia.service.WmChannelService;
import com.heima.wemedia.service.WmUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
public class WemediaClient implements IWemediaClient {
    @Autowired
    private WmUserService wmUserService;
    @Autowired
    private WmChannelMapper wmChannelMapper;

    @Autowired
    private GreenScanService greenScanService;

    /**
     * 根据传参 保存自媒体用户信息到wm_user表中
     *
     * @param wmUser
     * @return
     */
    @PostMapping("/api/v1/wm_user/save")
    @Override
    public ResponseResult saveWmUser(WmUser wmUser) {
        //1.入参校验
        if (wmUser == null || StringUtils.isBlank(wmUser.getName())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.查询用户是否已经存在,如果存在则直接返回失败,如果不存在则插入数据库表
        WmUser dbWmUser =
                wmUserService.getOne(Wrappers.<WmUser>lambdaQuery().eq(WmUser::getName, wmUser.getName()));
        if (dbWmUser != null) {
            //已经存在该用户名的用户
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "该用户名在自媒体端已存在!");
        }
        wmUserService.save(wmUser);
        //3.返回结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 根据id查询自媒体用户信息
     *
     * @param id
     * @return
     */
    @GetMapping("/api/v1/wm_user/findWmUserById")
    @Override
    public ResponseResult findWmUserById(Integer id) {
        //1入参校验
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmUser wmUser = wmUserService.getOne(Wrappers.<WmUser>lambdaQuery().eq(WmUser::getId, id));
        return ResponseResult.okResult(JSON.toJSONString(wmUser));
    }


    /**
     * 查询所有频道数据
     *
     * @return
     */
    @GetMapping("/api/v1/channel/list")
    public ResponseResult getChannels() {
        //频道数据应该先查询redis缓存中的数据,缓存没有再查数据库
        LambdaQueryWrapper<WmChannel> lqw = new LambdaQueryWrapper<>();
        lqw.orderByDesc(WmChannel::getCreatedTime);
        List<WmChannel> wmChannelList = wmChannelMapper.selectList(lqw);
        return ResponseResult.okResult(wmChannelList);
    }

    @Override
    @GetMapping("/api/v1/comment/content_scan")
    public Boolean isTextScanOk(@RequestParam String text) {
        return greenScanService.isTextScanOk(text);
    }
}
